package com.zfast.config.controller;

import com.zfast.config.constant.SessionKeyConst;
import com.zfast.config.entity.User;
import com.zfast.config.entity.Role;
import com.zfast.config.service.AdminService;
import com.zfast.config.service.RoleService;
import com.zfast.config.vo.CommonResult;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Classname DispatherController
 * @Description 控制器
 * @Date 2019/7/16 8:36
 * @Created by WDD
 */
@Controller
@RequestMapping("/badminton")
public class DispatherController {

    @Resource
    private CommonResult commonResult;
    @Resource
    private AdminService adminService;
    @Resource
    private RoleService roleService;
    @Resource
    private BCryptPasswordEncoder passwordEncoder;

    /**
     * 跳转登录界面
     *
     * @return
     */
    @GetMapping("/login")
    public String login() {
        return "badminton/login";
    }

    /**
     * 登出
     *
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "badminton/login";
    }

    /**
     * 跳转修改密码页面
     *
     * @return
     */
    @GetMapping("/password")
    public String password() {
        return "badminton/common/password";
    }

    /**
     * 修改密码
     *
     * @param session
     * @param password
     * @param newpassword
     * @param repassword
     * @return
     */
    @PostMapping("/editPassword")
    @ResponseBody
    public CommonResult editPassword(HttpSession session, String password, String newpassword, String repassword) {
        User user = (User) session.getAttribute(SessionKeyConst.USER);
        User user1 = adminService.selectById(user.getId());
        boolean matches = passwordEncoder.matches(password, user1.getPassword());

        if (!matches) {
            commonResult.ajaxFalse("原密码错误");
            return commonResult;
        }
        if (!newpassword.equals(repassword)) {
            commonResult.ajaxFalse("密码不一致");
            return commonResult;
        }
        user.setPassword(passwordEncoder.encode(newpassword));
        int count = adminService.editByAdmin(user);
        if (count >= 1) {
            commonResult.ajaxTrue("修改密码成功");
        } else {
            commonResult.ajaxFalse("系统错误");
        }
        return commonResult;
    }

    @GetMapping("/info")
    public String info(HttpSession session, Model model) {
        User user = (User) session.getAttribute(SessionKeyConst.USER);
        Role role = roleService.selectById(user.getRid());
        model.addAttribute(SessionKeyConst.ROLE, role);
        return "badminton/common/info";
    }

    @PostMapping("/editInfo")
    @ResponseBody
    public CommonResult editInfo(User user, HttpSession session) {
        User ad = (User) session.getAttribute(SessionKeyConst.USER);
        ad.setUsername(user.getUsername());
        ad.setEmail(user.getEmail());
        ad.setPhone(user.getPhone());
        int count = adminService.editByAdmin(ad);
        if (count >= 1) {
            commonResult.ajaxTrue("修改成功");
        } else {
            commonResult.ajaxFalse("修改失败");
        }
        return commonResult;
    }

}
